|
|
|
|
|
|
|
Listing 16.1. Creating the Employees table. |
|
|
|
|
|
|
|
|
CREATE TABLE Employees
(EmpID TEXT(5),
EmpName TEXT(30),
DeptID TEXT(5),
DeptName TEXT(20),
DeptLocation TEXT(20),
CONSTRAINT PKEmpID PRIMARY KEY (EmpID)); |
|
|
|
|
|
|
|
|
The EmpID field has been designated as a primary key field. This guarantees that no two records in the Employees data table can have the same EmpID value. You can use the EmpID field in the next table you create (the Skills table) as the reference field that links the two tables. Because you are using the EmpID field as a link, it must be a unique value in the Employees table in order to maintain database integrity. What you are doing here is setting up a one-to-many relationship between the Employees table (the one-side) and the Skills table (the many-side). Any time you establish a one-to-many relationship, you must make sure that the reference field (in this case, the EmpID field) is unique on the one-side of the relationship. |
|
|
|
|
|
|
|
|
Now that you have built the Employees table, you can create the table that holds all the skills data. Use the information in Table 16.1 to write an SQL DDL statement that creates a table called Skills. Make sure the new table has the field EmpID and that the EmpID field is built with the correct index constraint to enforce one-to-many database integrity. Your SQL statement should look like the one in Listing 16.2. |
|
|
|
|
|
|
|
|
Listing 16.2. Creating the Skills table. |
|
|
|
|
|
|
|
|
CREATE TABLE Skills
(EmpID TEXT(5),
SkillCode TEXT(5),
SkillName TEXT(20),
SkillDeptID TEXT(5),
SkillLevel INTEGER,
CONSTRAINT PKSkills PRIMARY KEY (SkillCode,EmpID),
CONSTRAINT FKEmpID FOREIGN KEY (EmpID) REFERENCES Employees(EmpID)); |
|
|
|
|
|
|
|
|
You can see in Listing 16.2 that you have used the FOREIGN KEY_REFERENCES syntax to establish and maintain the table relationship. As you remember from the SQL lessons on Day 13 and Day 15, the FOREIGN KEY_REFERENCES syntax makes sure that any entry in the Skills.EmpID field can be found in the related Employees.EmpID field. If users enter a value in the Skills.EmpID field that cannot be found in any Employees.EmpID field, Visual Basic automatically issues a database error message. This message is generated by Visual Basic, not by your program. |
|
|
|
|
|